﻿using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using UnityEngine;
namespace HBFramework
{
    public partial class AudioManager
    {
        private Dictionary<string, AudioClip> audioClipDict;

        public async UniTask LoadAudioClips(Dictionary<string, string> dict)
        {
            audioClipDict ??= new Dictionary<string, AudioClip>();
            foreach (var item in dict)
            {
                if (audioClipDict.ContainsKey(item.Key))
                    continue;
                var clip = await AssetManagerProxy.Instance.LoadAsset<AudioClip>(item.Value);
                clip.LoadAudioData();
                audioClipDict.Add(item.Key, clip);
            }
        }

        public void UnloadAudioClips(Dictionary<string, string> dict)
        {
            if (audioClipDict == null)
                return;
            foreach (var item in dict)
            {
                var clip = audioClipDict[item.Key];
                clip.UnloadAudioData();
                audioClipDict.Remove(item.Key);
            }
        }

        public AudioPlayer PlaySound(string key, float volume = 1)
        {
            if (!audioClipDict.ContainsKey(key))
            {
                Debug.LogError($"音频 {key} 未加载");
                return null;
            }
            return PlaySound(audioClipDict[key], volume);
        }

        public AudioPlayer PlayMusic(string key, float volume = 1)
        {
            if (!audioClipDict.ContainsKey(key))
            {
                Debug.LogError($"音频 {key} 未加载");
                return null;
            }
            return PlayMusic(audioClipDict[key], volume);
        }
    }
}